package com.fengmi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.entity.MallGoodsTemplate;
import com.fengmi.mapper.MallGoodsTemplateMapper;
import com.fengmi.service.IMallGoodsTemplateService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 作者：陈学虎
 * 时间：2021/7/9 0009 下午 4:10
 */
@Service
public class MallGoodsTemplateService implements IMallGoodsTemplateService {

    @Autowired
    private MallGoodsTemplateMapper templateMapper;

    @Override
    public PageResultVO<MallGoodsTemplate> findPage(PageDTO<MallGoodsTemplate> pageDTO) {
        if (pageDTO == null){
            return new PageResultVO<>(false,"请求参数错误");
        }

        PageHelper.startPage(pageDTO.getPage(),pageDTO.getSize());

        List<MallGoodsTemplate> templates = templateMapper.findPage(pageDTO.getWhere());

        PageInfo<MallGoodsTemplate> pageInfo = new PageInfo<>(templates);

        PageResultVO<MallGoodsTemplate> pageResultVO = new PageResultVO<>(true,"success");

        pageResultVO.setData(pageInfo.getList());
        pageResultVO.setPages(pageInfo.getPages());
        pageResultVO.setTotal(pageInfo.getTotal());

        return pageResultVO;
    }

    @Override
    public ResultVO save(MallGoodsTemplate mallGoodsTemplate) {
        if(ObjectUtil.isEmpty(mallGoodsTemplate)){
            return new ResultVO(false,"请求参数错误");
        }
        int i = templateMapper.save(mallGoodsTemplate);
        return new ResultVO(i==1?true:false,i==1?"保存成功":"保存失败");
    }

    @Override
    public List<MallGoodsTemplate> findAllTemp() {

        return templateMapper.findAllTemp();
    }
}
